<?php

namespace App\AdminModule\Presenters;

use \App\Model\Dao\VideoComment,
    \App\Model\Dao\VideoView,
    \App\Model\Dao\Video,
    \Grido, 
    \Grido\Grid,
    \Nette\Application\UI\Form;
/**
 * Video presenter.
 */
class VideoPresenter extends BasePresenter {
 
  /** @var \App\Model\VideoManager @inject */
  public $vm;
  
  /** @var \Kdyby\Doctrine\EntityManager @inject */
  public $em;
  
  private $video;
  
  /**
   * Add action
   * @param int $id
   * @return type
   */
  public function actionAdd($id){
    if(!isset($id)){
      return;
    }
    
    $this->video = $this->vm->getVideoById($id);
  }
  
  /**
   * Add video form
   * @param type $name
   * @return \Nette\Application\UI\Form
   */
  public function createComponentAddVideoForm($name){
    $form = new Form();
    $form->addText('code', 'Kód');
    $form->addText('category', 'Kategorie');
    $form->addText('title', 'Nadpis');
    $form->addText('link', 'Link');
    $form->addTextArea('description', 'Popis');
    $form->addText('requirements', 'Požadavky');
    $form->addText('duration', 'Délka');
    $form->addText('keyword', 'Klíčová slova');
    $form->addText('learn', 'Učící křivka');
    $form->addText('style', 'Styl');
    $form->addText('level', 'Level');
    $form->addText('webdesc', 'Webový description');

    if($this->video){
      $form['code']->setDefaultValue($this->video->getCode());
      $form['category']->setDefaultValue($this->video->getCategory());
      $form['title']->setDefaultValue($this->video->getTitle());
      $form['link']->setDefaultValue($this->video->getWebalize());
      $form['description']->setDefaultValue($this->video->getDescription());
      $form['requirements']->setDefaultValue($this->video->getRequirements());
      $form['duration']->setDefaultValue($this->video->getDuration());
      $form['keyword']->setDefaultValue($this->video->getKeyword());
      $form['learn']->setDefaultValue($this->video->getLearn());
      $form['style']->setDefaultValue($this->video->getStyle());
      $form['level']->setDefaultValue($this->video->getLevel());
      $form['webdesc']->setDefaultValue($this->video->getWebdesc());
    }

    $form->addSubmit('send');
    $form->onSuccess[] = callback($this, 'addVideoFormSuccess');

    return $form;
  }  
  
  /**
   * Add video form success
   * @param Form $form
   */
  public function addVideoFormSuccess($form){
    $values = $form->getValues();

    if(isset($this->video)){
      $video = $this->video;
    }else{
      $video = new Video();
    }
     
    $video->setCode($values['code']);
    $video->setCategory($values['category']);
    $video->setWebalize($values['link']);
    $video->setTitle($values['title']);
    $video->setDescription($values['description']);
    $video->setRequirements($values['requirements']);
    $video->setDuration($values['duration']);
    $video->setKeyword($values['keyword']);
    $video->setLearn($values['learn']);
    $video->setStyle($values['style']);
    $video->setLevel($values['level']);
    $video->setWebdesc($values['webdesc']);
    $this->em->persist($video);
    $this->em->flush();

    $this->flashMessage('Video bylo uloženo', 'success');
    $this->redirect('Video:default');
  }
  
  /**
   * Handle delete video
   * @param int $id
   */
  public function handleDelete($id){
    $video = $this->vm->getVideoById($id);
    
    if($video){
      // remove comments
      $comments = $this->em->getDao(VideoComment::getClassName())->findBy(array('video' => $video));
      $this->em->remove($comments);
      
      // remove views
      $views = $this->em->getDao(VideoView::getClassName())->findBy(array('video' => $video));
      $this->em->remove($views);
      
      // remove video
      $this->em->remove($video);
      $this->em->flush();
    }
    
    $this->flashMessage('Video bylo smazáno', 'success');
    $this->redirect('Video:default');
  }
  
  /**
   * Video grid
   * @param String $name
   * @return \Grido\Grid
   */
  public function createComponentVideoGrid($name){
    $g = new Grid($this, $name);

    // Grido data
    $g->model = $this->vm->getAllVideosBaseArray();    
    
    $g->addColumnText('id', 'ID')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('title', 'Učící křivka')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('category', 'Kategorie')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('webalize', 'Link')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('duration', 'Délka')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('learn', 'Učící křivka')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('views', 'Sledovanost')
        ->setSortable()
        ->setFilterText();
    $g->addColumnText('comments', 'komentáře')
        ->setSortable()
        ->setFilterText();
    

    // Create edit button
    $g->addActionHref('edit', 'Edit', 'Video:add')
      ->setIcon('pencil')->setPrimaryKey('id');

    // Create edit button
    $g->addActionHref('delete', 'Smazat', 'delete!')
      ->setIcon('remove')->setPrimaryKey('id');
     
    // Return grido
    $g->filterRenderType = Grido\Components\Filters\Filter::RENDER_INNER;
    $g->setExport();
    return $g;
  }
}
